const { Builder, By, until } = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");

(async function myFunction() {
	let driver = await new Builder()
		.forBrowser("chrome")
		.setChromeOptions(new chrome.Options().addArguments(["--start-maximized"]))
		.build();

	// 导航到某个网站
	await driver.get("http://172.16.1.138:3100/");

	await handleLogin(driver);
	// await handleAdd(driver);
})();

async function handleLogin(driver) {
	await driver.wait(until.titleContains("登录"), 10000);

	await driver.findElement(By.id("account")).sendKeys("jhe");
	await driver.findElement(By.id("password")).sendKeys("123456");

	let spanElement = await driver.findElement(By.css("button"));
	spanElement.click();

	await handleSearch(driver);
}

async function handleSearch(driver) {
	await driver.wait(until.titleContains("应用列表"), 10000);

	const parentElement = await driver.findElement(By.id("appName"));
	// 在找到的父元素中定位子<input>元素
	let inputElement = await parentElement.findElement(By.css("input"));
	// 给<input>框赋值
	await inputElement.sendKeys("196地质大队");
	// 根据name属性定位按钮

	let find = await driver.findElement(By.css('button[text="查询"]'));
	await find.click();

	setTimeout(async () => {
		let reset = await driver.findElement(By.css('button[text="重置"]'));
		await reset.click();

		setTimeout(async () => {
			await handleAdd(driver);
		}, 2000);
	}, 2000);
}

async function handleAdd(driver) {
	await driver.get("http://172.16.1.138:3100/#/Configuration/baseMap");

	await driver.wait(until.titleContains("底图配置"), 10000);

	// return;

	let add = await driver.findElement(
		By.css('button[preicon="ant-design:plus-outlined"]')
	);

	await add.click();

	// await driver.wait(driver.findElement(By.id("mapName")), 10000);
	//
	const dom = await driver.wait(
		until.elementIsVisible(
			driver.findElement(By.id("mapName")).findElement(By.css("input"))
		),
		10000
	);
	dom.sendKeys("testMap");
	await driver
		.findElement(By.id("mapUrl"))
		.findElement(By.css("input"))
		.sendKeys("test");
	//

	const buttons = await driver.findElements(By.tagName("button"));
	// const map = driver.executeScript("return arguments[0].outerHTML;", buttons);

	for (let button of buttons) {
		let span = await button.findElement(By.tagName("span"));
		let spanText = await span.getText();

		if (spanText === "确 认") {
			await button.click();
			setTimeout(async () => {
				await handleRemove(driver);
			}, 2000);
		}
	}
}

async function handleRemove(driver) {
	const trs = await driver.findElements(By.tagName("tr"));
	console.log("trs", trs.length);
	for (let tr of trs) {
		let tds = await tr.findElements(By.tagName("td"));
		// console.log("div", div.length);

		for (let td of tds) {
			let div = await td.findElement(By.tagName("div"));
			let divText = await div.getText();
			// console.log("div", divText);

			if (divText === "testMap") {
				let deleteTds = await tr.findElements(By.tagName("td"));
				for (let deleteTd of deleteTds) {
					let deleteDiv = await deleteTd.findElement(By.tagName("div"));
					let deleteDivText = await deleteDiv.getText();

					if (deleteDivText === "编辑删除") {
						// console.log("deleteDiv", deleteDiv);
						const as = await deleteDiv.findElements(By.tagName("a"));
						for (let a of as) {
							let aText = await a.getText();
							if (aText === "删除") {
								await a.click();

								setTimeout(async () => {
									let spanElementWithText = await driver.findElement(
										By.xpath("//span[contains(text(), '确 定')]")
									);
									spanElementWithText.click();
								}, 2000);
								// console.log("spanElementWithText", spanElementWithText);
							}
						}
					}
				}
			}
		}
		// let divText = await div.getText();
		// console.log("divText", divText);
	}
}
